package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.CollectionMapper;
import cn.scxy.bookcity.pojo.dto.CollectionDTO;
import cn.scxy.bookcity.pojo.entity.Collection;
import cn.scxy.bookcity.pojo.vo.CollectionListItemVO;
import cn.scxy.bookcity.service.ICollectionService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CollectionServiceImpl implements ICollectionService {

    @Autowired
    CollectionMapper collectionMapper;

    @Override
    public List<CollectionListItemVO> list(Long uId) {
        log.debug("开始处理查询收藏业务");
        List<CollectionListItemVO> list = collectionMapper.list(uId);
        for (CollectionListItemVO collectionListItemVO : list) {
            Long gId = collectionListItemVO.getGId();
            Integer IgId=Long.valueOf(gId).intValue();
            String url = collectionMapper.getUrl(IgId);
            collectionListItemVO.setUrl(url);
        }
        return list;
    }

    @Override
    public void addNew(CollectionDTO collectionDTO) {

        Collection collection = new Collection();
        BeanUtils.copyProperties(collectionDTO,collection);
        int rows = collectionMapper.addNew(collection);
        if (rows!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"插入的信息错误");
        }
    }

}
